﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Microsoft.Practices.Unity;
using Wyn.PMS.Common.Utils;
using Wyn.PMS.Entity.DataAccess;
using Wyn.PMS.Entity.Entities;
using Wyn.PMS.Entity.IDataAccess;

namespace Wyn.PMS.Common.NightAudit
{
    internal class CompleteAudit
    {
        private static readonly IUnityContainer container = UnityContainerService.GetContainer();

        public void Complete(IList<Account> notRollAccounts,IList<Order> orders, HotelBizDay bizDay)
        {
            string roomNos = BuildUpdateRooms(orders);

            IRoomRepository roomRepository = container.Resolve<IRoomRepository>();

            string rsOD = EnumHelper.GetCode(RoomStatus.OD);

            using(TransactionScope scope=new TransactionScope())
            {
                //更新过租 账目 IsRentRoll
                RollAccounts(bizDay.HotelID, notRollAccounts);

                //更新房间状态
                roomRepository.UpdateRooms(bizDay.HotelID, roomNos, rsOD);

                TurnBizDay(bizDay);

                scope.Complete();
            }

        }

        public void Complete(IList<Account> notRollAccounts, HotelBizDay bizDay)
        {
            //更新过租 账目 IsRentRoll
            if (bizDay.Status != EnumHelper.GetCode(NightAuditCode.Status.E)
                || (bizDay.Status == EnumHelper.GetCode(NightAuditCode.Status.E)
                && bizDay.Steping == (int)NightAuditCode.Steping.CompleteAudit_A))
            {
                bizDay.Status = EnumHelper.GetCode(NightAuditCode.Status.I);

                RollAccounts(bizDay.HotelID, notRollAccounts);
            }

        }

        public void Complete(IList<Order> orders, HotelBizDay bizDay)
        {
            IRoomRepository roomRepository = container.Resolve<IRoomRepository>();

            string rsOD = EnumHelper.GetCode(RoomStatus.OD);

            string roomNos = BuildUpdateRooms(orders);

            if (bizDay.Status != EnumHelper.GetCode(NightAuditCode.Status.E)
                || (bizDay.Status == EnumHelper.GetCode(NightAuditCode.Status.E)
                && bizDay.Steping == (int)NightAuditCode.Steping.CompleteAudit_B))
            {
                bizDay.Status = EnumHelper.GetCode(NightAuditCode.Status.I);

                //更新房间状态
                roomRepository.UpdateRooms(bizDay.HotelID, roomNos, rsOD);
            }
            
        }

        public void Complete(HotelBizDay bizDay)
        {
            TurnBizDay(bizDay);
        }
        
        private string BuildUpdateRooms(IList<Order> orders)
        {
            IEnumerable<string> rooms = orders.Select(r => r.RoomNo);

            string roomNos="'"+string.Join("','", rooms)+"'";

            return roomNos;
        }

        private void RollAccounts(string hotelId,IList<Account> notRollAccounts)
        {
            if(notRollAccounts!=null&&notRollAccounts.Count<=0)return;

            IAccountRepository accountRepository = container.Resolve<IAccountRepository>();

            foreach(var account in notRollAccounts)
            {
                account.IsRentRoll = true;
            }

            accountRepository.BatchUpdate(hotelId, notRollAccounts);
        }


        /// <summary>
        /// 营业日翻牌 
        /// </summary>
        private void TurnBizDay(HotelBizDay bizDay)
        {
            IHotelBizDayRepository bizDayRepository = container.Resolve<IHotelBizDayRepository>();

            bizDay.BizDay = bizDay.BizDay.AddDays(1);
            
            bizDay.AuditTime = DateTime.Now;

            bizDay.Status = EnumHelper.GetCode(NightAuditCode.Status.C);

            bizDayRepository.Update(bizDay);

        }
    }
}
